package com.kanshu.ksgb.fastread.doudou.module.bookcity.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.supertextview.SuperTextView;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.ItemRefreshBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.PageSelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.utils.BookCityUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

import static com.kanshu.ksgb.fastread.doudou.app.constants.Constants.BookCityModuleType.TYPE_FEMALE_FINISHED;
import static com.kanshu.ksgb.fastread.doudou.app.constants.Constants.BookCityModuleType.TYPE_MALE_FINISHED;

/**
 * Created by wcy on 18/5/23.
 */

public class AdSelectedRenqiFinishedLayout extends LinearLayout {
    @BindView(R.id.top_container)
    LinearLayout mTopContainer;
    @BindView(R.id.bottom_container)
    LinearLayout mBottomContainer;
    ItemRefreshBean mItemRefreshBean;
    @BindView(R.id.top)
    FrameLayout mTop;
    @BindView(R.id.bottom)
    FrameLayout mBottom;
    @BindView(R.id.divider)
    View mDivider;
    int mSite;
    Runnable mRunnable;
    @BindView(R.id.change_tip)
    ImageView mChangeTip;
    @BindView(R.id.change_bottom_tip)
    ImageView mChangeBottomTip;
    public Subject<Integer> lifeCyclerSubject = PublishSubject.create();
    BookCityPresenter presenter;

    public AdSelectedRenqiFinishedLayout(Context context) {
        super(context);
        init();
    }

    public AdSelectedRenqiFinishedLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdSelectedRenqiFinishedLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        presenter = new BookCityPresenter(lifeCyclerSubject);
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_ad_selected_renqi_finished_layout, this);
        ButterKnife.bind(view);
        mItemRefreshBean = new ItemRefreshBean();
        mItemRefreshBean.type = TYPE_MALE_FINISHED;
        mItemRefreshBean.selectedItem = new SelectedBean();
        mItemRefreshBean.selectedItem.type = mItemRefreshBean.type;
        mItemRefreshBean.selectedItem.page_info = new PageSelectedBean();
        mItemRefreshBean.selectedItem.page_info.cur_page = "1";
        mItemRefreshBean.selectedItem.page_info.num = "6";
        mItemRefreshBean.list = new ArrayList<>(1);
        mItemRefreshBean.list.add(mItemRefreshBean.selectedItem);
    }

    @OnClick({R.id.change_container, R.id.change_bottom_container})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.change_container:
                BookCityUtils.rotate(mChangeTip);
                BookCityUtils.refreshModuleInfos(mItemRefreshBean, new IRefresh<SelectedBean>() {
                    @Override
                    public void refresh(SelectedBean selectedBean, Object object) {
                        refreshUI(selectedBean);
                    }

                    @Override
                    public void onError(int code, String desc) {
                        BookCityUtils.rotateCancel(mChangeTip);
                    }

                    @Override
                    public void reset() {

                    }
                },presenter);
                break;
            case R.id.change_bottom_container:
                if (mRunnable != null) {
                    BookCityUtils.rotate(mChangeBottomTip);
                    mRunnable.run();
                }
                break;
            default:
                break;
        }
    }

    public void cancelChangeBottomRoate() {
        BookCityUtils.rotateCancel(mChangeBottomTip);
    }

    public void setBottomChangeCallback(Runnable runnable) {
        mRunnable = runnable;
    }

    private void refreshItem(int pos, LinearLayout container, BookInfo item) {
        container.getChildAt(pos).setVisibility(View.VISIBLE);
        FrameLayout frameLayout = (FrameLayout) container.getChildAt(pos);
        LinearLayout layout = (LinearLayout) frameLayout.getChildAt(0);
        ImageView imageView = (ImageView) layout.getChildAt(0);
        TextView tv1 = (TextView) layout.getChildAt(1);
        SuperTextView tv2 = (SuperTextView) layout.getChildAt(2);
        GlideImageLoader.load(item.cover_url, imageView);
        tv1.setText(item.book_title);
        tv2.setText(BookUtils.getBookLabel(item));
        layout.setOnClickListener(v -> {
            ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", item.book_id);
        });
    }

    private void refreshUI(SelectedBean selectedBean) {
        BookCityUtils.rotateCancel(mChangeTip);
        if (Utils.isEmptyList(selectedBean.list)) {
            DisplayUtils.gone(mTop, mTopContainer, mBottomContainer, mDivider);
        } else {
            DisplayUtils.visible(mTop, mTopContainer, mBottomContainer, mDivider);
            for (int i = 0; i < mTopContainer.getChildCount(); i++) {
                mTopContainer.getChildAt(i).setVisibility(INVISIBLE);
            }
            for (int i = 0; i < mBottomContainer.getChildCount(); i++) {
                mBottomContainer.getChildAt(i).setVisibility(INVISIBLE);
            }
            if (selectedBean.list.size() <= 3) {
                DisplayUtils.gone(mBottomContainer);
            }
            if (selectedBean.list.size() > 6) {
                selectedBean.list = selectedBean.list.subList(0, 6);
            }
            for (int i = 0; i < selectedBean.list.size(); i++) {
                if (i < 3) {
                    refreshItem(i, mTopContainer, selectedBean.list.get(i));
                } else {
                    refreshItem(i - 3, mBottomContainer, selectedBean.list.get(i));
                }
            }
        }
    }

    public void refresh(int site) {
        if (site != mSite) {
            mItemRefreshBean.selectedItem.page_info.cur_page = "0";
        }
        mSite = site;
        mItemRefreshBean.selectedItem.type = site == 1 ? TYPE_MALE_FINISHED : TYPE_FEMALE_FINISHED;
        BookCityUtils.refreshModuleInfos(mItemRefreshBean, null, new INetCommCallback<SelectedBean>() {
            @Override
            public void onResponse(SelectedBean selectedBean) {
                refreshUI(selectedBean);
            }

            @Override
            public void onError(int code, String errDesc) {
                DisplayUtils.gone(mTop, mTopContainer, mBottomContainer, mDivider);
            }
        },presenter);
    }
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //lifeCyclerSubject.onNext(ACTION_DESTORY);
    }
}
